#include "stdafx.h"

#include "memory.h"

#include "basicInformation.h"
#include "TSearcherWindow.h"
#include "ui_tsearcher.h"
#include "TCpu.h"
#include "ItemsSearcher.h"

#include "TaskManager.h"
#include "ItemBrowser.h"
#include "HelperFunctions.h"
#include "Constants.h"

TaskManager::TaskManager(TSearcherWindow *ts)
    : QObject(ts)
{
    // get ui
    auto ui = ts->ui;

    // common arguments
    this->supportKeyword = ui->supportKeywordEdit->text();
    this->scrollInterval = ui->scrollIntervalEdit->text().toDouble();

    // searcher arguments
    itemsSearcher.reset(new ItemsSearcher(this));
    this->itemKeyword = ui->itemKeywordEdit->text();
    this->itemLowerPrice = ui->itemLowerPriceEdit->text();
    this->itemHigherPrice = ui->itemHigherPriceEdit->text();
    this->sortBy = ts->sortDescript();
    this->itemTitle = ui->itemTitleEdit->text();
    this->itemPrice = ui->itemPriceEdit->text();
    this->itemShopName = ui->itemShopNameEdit->text();
    this->location = ui->itemLocationEdit->text();
    this->itemId = ui->itemIdEdit->text();
    this->sendItemSelectedCapture = ui->sendItemSelectedCaptureCheckBox->isChecked();
    this->searchMethod = ts->searchMethod();

    // browser arguments
    this->mainBrowserConfig = ui->mainItemsConfigurationWidget->toConfig();
    this->subBrowserConfig = ui->subItemsConfigurationWidget->toConfig();
    this->compareBrowserConfig = ui->compareItemsConfigurationWidget->toConfig();
    this->collectShopCapture = ui->collectShopCaptureCheckBox->isChecked();
    this->shopIndexCapture = ui->shopIndexCaptureCheckBox->isChecked();

    // basic information arguments
    this->ip = ui->ipCheckBox->isChecked();
    this->trades = ui->tradesCheckBox->isChecked();
    this->rate = ui->rateCheckBox->isChecked();
    this->nickname = ui->nicknameCheckBox->isChecked();
    this->registerDate = ui->registerDateCheckBox->isChecked();
}
TaskManager::~TaskManager()
{
}

void TaskManager::start()
{
    if (this->ip || this->trades || this->rate || this->nickname || this->registerDate)
    {
        basic.reset(new BasicInformation(this));
        basic->ip = this->ip;
        basic->trades = this->trades;
        basic->rate = this->rate;
        basic->nickname = this->nickname;
        basic->registerDate = this->registerDate;
        basic->supportKeyword = this->supportKeyword;
        QObject::connect(basic.get(), &BasicInformation::finished, this, &TaskManager::on_fetchBasicInformation_finished, Qt::QueuedConnection);
        basic->start();
    }
    else
    {
        on_fetchBasicInformation_finished();
    }
    playSound(Constants::audioPath.taskStartd);
}

void TaskManager::on_fetchBasicInformation_finished()
{
    if (this->itemKeyword.length() > 0)
    {
        itemsSearcher.reset(new ItemsSearcher(this));
        itemsSearcher->itemKeyword = this->itemKeyword;
        itemsSearcher->itemLowerPrice = this->itemLowerPrice;
        itemsSearcher->itemHigherPrice = this->itemHigherPrice;
        itemsSearcher->sortBy = this->sortBy;
        itemsSearcher->itemTitle = this->itemTitle;
        itemsSearcher->itemPrice = this->itemPrice;
        itemsSearcher->itemShopName = this->itemShopName;
        itemsSearcher->location = this->location;
        itemsSearcher->itemId = this->itemId;
        itemsSearcher->sendItemSelectedCapture = this->sendItemSelectedCapture;
        itemsSearcher->supportKeyword = this->supportKeyword;
        itemsSearcher->searchMethod = this->searchMethod;
        itemsSearcher->start();

        QObject::connect(itemsSearcher.get(), &ItemsSearcher::finished, []()
        {
            playSound(Constants::audioPath.taskFinished);
        });
    }
}
